package net.xdclass.onlinexdclass.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.xdclass.onlinexdclass.aop.interfaceaop.SysLog;
import net.xdclass.onlinexdclass.model.entity.User;
import net.xdclass.onlinexdclass.model.request.LoginRequest;
import net.xdclass.onlinexdclass.service.UserService;
import net.xdclass.onlinexdclass.utils.JsonDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(value = "用户接口")
@RestController
@RequestMapping("/api/v1/pri/user")
public class UserController {


    @Autowired
    private UserService userService;






    @ApiOperation(value = "注册接口")
    @PostMapping("/register")
    public JsonDate register(@RequestBody Map<String, String> userInfo) {

        if (null != userInfo) {
            int rows = userService.save(userInfo);

            return rows == 1 ? JsonDate.bulidSuccess() : JsonDate.bulidError("注册失败请重试");
        } else {
            return JsonDate.bulidError("注册失败请重试");
        }

    }


    /**
     * aop测试接口
     *
     * @param aopmessage
     * @return
     */
    @ApiOperation(value = "aop测试接口")
    @GetMapping("/aoptest")
    public JsonDate aopTest(String aopmessage) {
        return JsonDate.bulidSuccess(aopmessage);
    }

    /**
     * aop日志注解测试接口
     */
    @ApiOperation(value = "aop注解方式测试接口")
    @SysLog("测试")
    @GetMapping("/test")
    public String test(@RequestParam("name") String name, @RequestParam("age") int age) {
        return name + ", " + age;
    }

    /**
     * 登录接口
     *
     * @param loginRequest
     * @return
     */
    @ApiOperation(value = "登录接口，同时也是用于获取token")
    @PostMapping("/login")
    public JsonDate login(@RequestBody LoginRequest loginRequest) {
        String token = userService.findByPhoneAndPwd(loginRequest.getPhone(), loginRequest.getPwd());

        return token == null ? JsonDate.bulidError("账户密码 错误") : JsonDate.bulidSuccess(token);

    }


    /**
     * 根据id查询User对象
     */
    @ApiOperation(value = "根据id查询User对象")
    @GetMapping("/{id}")
    public JsonDate getUserById(@PathVariable(value = "id") Integer id) {
        if(null != id) {
            User userById = userService.getUserById(id);
            return JsonDate.bulidSuccess(userById);
        }else {
            return JsonDate.bulidError("错误");
        }
    }


    /**
     * 更新user
     */
    @ApiOperation(value = "更新user")
    @PutMapping("/updateUser")
    public JsonDate updateUser(@RequestBody User user) {
        userService.updateUser(user);
        return JsonDate.bulidSuccess();
    }


    /**
     *  根据Token获取用户信息
     */
    @ApiOperation(value = "根据Token获取用户信息")
    @GetMapping("/find_user_token")
    public JsonDate findUserByToken(HttpServletRequest request) {

        Integer user_id = (Integer) request.getAttribute("user_id");

        if(user_id == null) {
            return JsonDate.bulidError("查询失败");
        }
        User userByToken = userService.findUserByToken(user_id);
        if(null == userByToken) {
            return JsonDate.bulidError("查询失败");
        }
        return JsonDate.bulidSuccess(userByToken);
    }



}
